package com.aygxy.carproject.controller;

import com.aygxy.carproject.pojo.order.dto.OrderAddDTO;
import com.aygxy.carproject.pojo.order.dto.OrderListTimeDTO;
import com.aygxy.carproject.pojo.order.dto.OrderStateUpdateDTO;
import com.aygxy.carproject.pojo.order.vo.OrderAddVO;
import com.aygxy.carproject.pojo.order.vo.OrderListVO;
import com.aygxy.carproject.service.IOmsOrderService;
import com.aygxy.carproject.web.JsonPage;
import com.aygxy.carproject.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "04. 订单管理模块")
@RestController
@Slf4j
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private IOmsOrderService omsOrderService;

    @PostMapping("/add")
    @ApiOperation("执行新增订单的方法")
    //@PreAuthorize("hasRole('user')")
    public JsonResult<OrderAddVO> addOrder(@Validated OrderAddDTO orderAddDTO){
        OrderAddVO orderAddVO=omsOrderService.addOrder(orderAddDTO);
        return JsonResult.ok(orderAddVO);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询当前用户指定时间的订单")
    //@PreAuthorize("hasRole('user')")
    public JsonResult<JsonPage<OrderListVO>> listUserOrders(
            OrderListTimeDTO orderListTimeDTO){
        JsonPage<OrderListVO> jsonPage=
                omsOrderService.listOrdersBetweenTimes(orderListTimeDTO);
        return JsonResult.ok(jsonPage);
    }

    @PostMapping("/update/state")
    @ApiOperation("修改订单状态的方法")
    //@PreAuthorize("hasRole('user')")
    public JsonResult updateOrderState(
            @Validated OrderStateUpdateDTO orderStateUpdateDTO){
        omsOrderService.updateOrderState(orderStateUpdateDTO);
        return JsonResult.ok("修改完成!");
    }
}
